using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class SkinApplicator : MonoBehaviour
{
	public bool IsOpponent;

	public Transform HeadParent;

	public GameObject HeadObject;

	public Transform[] HeadParents;

	public List<GameObject> HeadObjects = new List<GameObject>();

	public List<MeshRenderer> Cylinders = new List<MeshRenderer>();

	public List<MeshRenderer> OutsideJoints = new List<MeshRenderer>();

	public List<MeshRenderer> InsideJoints = new List<MeshRenderer>();

	public Transform DrillJoint;

	private void Start()
	{
		ApplySkin();
		InventoryManager instance = InventoryManager.Instance;
		instance.OnSkinChanged = (Action)Delegate.Combine(instance.OnSkinChanged, new Action(ApplySkin));
	}

	private async void StartDropAnimation()
	{
		if (HeadObject != null)
		{
			HeadObject.transform.localPosition = new Vector3(0f, 30f, 0f);
		}
		await Task.Delay(500);
		if (HeadObject != null)
		{
			NintendoPlayerPrefs.SetInt("Play_Transition", 0);
			HeadObject.transform.DOLocalMove(new Vector3(0f, 0f, 0f), 2f).SetEase(Ease.InSine).OnComplete(delegate
			{
				HeadObject.transform.DOPunchScale(HeadObject.transform.localScale * 1.01f, 0.5f, 2);
			});
		}
	}

	private void OnDestroy()
	{
		InventoryManager instance = InventoryManager.Instance;
		instance.OnSkinChanged = (Action)Delegate.Remove(instance.OnSkinChanged, new Action(ApplySkin));
	}

	public void ApplySkin()
	{
		Item item = InventoryManager.Instance.CurrentlySelected(ItemType.Skin);
		if (IsOpponent)
		{
			if (SingletonMB<DuelModeManager>.Instance.DuelStarted)
			{
				item = InventoryManager.Instance.GetItem(SingletonMB<DuelModeManager>.Instance.OpponentSkin);
			}
			else
			{
				item = InventoryManager.Instance.GetRandomItemOfType(ItemType.Skin);
				SingletonMB<DuelModeManager>.Instance.OpponentSkin = item.ID;
			}
		}
		if (HeadObject != null)
		{
			UnityEngine.Object.Destroy(HeadObject);
		}
		if (Preloader.ShopConfig.SkinWithUniqueIncrement)
		{
			NintendoPlayerPrefs.SetInt("ScinSize", item.SizePercent);
			NintendoPlayerPrefs.SetInt("ScinOil", item.FuelPercent);
			NintendoPlayerPrefs.SetInt("ScinPower", item.PowerPercent);
		}
		UnityEngine.Object.FindObjectOfType<Oil>()?.UpdateValues();
		GameObject headObject = item.HeadObject;
		HeadObject = UnityEngine.Object.Instantiate(headObject, HeadParent);
		HeadObject.transform.localPosition = Vector3.zero;
		for (int i = 0; i < HeadObjects.Count; i++)
		{
			UnityEngine.Object.Destroy(HeadObjects[i]);
		}
		for (int j = 0; j < HeadParents.Length; j++)
		{
			GameObject headObject2 = item.HeadObject;
			HeadObjects.Add(UnityEngine.Object.Instantiate(headObject2, HeadParents[j]));
			headObject2.transform.localPosition = Vector3.zero;
		}
		foreach (MeshRenderer cylinder in Cylinders)
		{
			cylinder.material = item.CylindarMaterial;
		}
		foreach (MeshRenderer outsideJoint in OutsideJoints)
		{
			outsideJoint.material = item.JointOuter;
		}
		foreach (MeshRenderer insideJoint in InsideJoints)
		{
			insideJoint.material = item.JointCenter;
		}
		if (!IsOpponent && Preloader.Transition.IsEnabled && NintendoPlayerPrefs.GetInt("Play_Transition", 0) == 1)
		{
			HeadParent.gameObject.SetActive(value: false);
		}
	}
}
